/*
 * PuzzleShape - place walls like in Tetris. or not
 *
 * Copyright 2007 James W. Hofmann
 *
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */

import lib3f.LCG;

#if flash9
import flash.display.Sprite;
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.geom.Point;
import flash3f.Images;
class PuzzleShape extends Sprite
#else neko
class PuzzleShape
#end
{
	static public var shapes = [
	
		[
			[2]
		],
	
		[
			[1,0],
			[2,1]
		],
	
		[
			[0,1,0],
			[0,2,0],
			[0,1,0]
		],

		[
			[0,1,0],
			[0,2,1],
			[0,1,0]
		],

		[
			[2,1],
			[1,1]			
		],

		[
			[0,1,0],
			[0,2,0],
			[0,1,1]
		],

		[
			[0,1,0],
			[0,2,0],
			[1,1,0]
		]
		
	];

	public var myshape : Array <Array<Int>>;
	
	public function new()
	{
		//myshape = shapes[LCG.randInt(6)].copy();
		myshape = shapes[0].copy();
		#if flash9
		super();
		drawoutline();
		#end
	}
	
	public function replace()
	{
		//myshape = shapes[LCG.randInt(6)].copy();
		myshape = shapes[0].copy();
		#if flash9
		drawoutline();
		#end
	}
	
	public function rotate()
	{
		var newshape : Array<Array<Int>> = new Array();
		
		for (y in 0...myshape.length)
		{
			var newline : Array<Int> = new Array();
			for (x in 0...myshape[0].length)
			{
				newline.push(myshape[x][myshape.length-1-y]);
			}
			newshape.push(newline);
		}
		myshape = newshape;
	}	
	
	public function drawoutline()
	{

		#if flash9
	
		for (n in 0...numChildren)
		{
			removeChildAt(0);
		}
	
		var basetbad : Bitmap = Images.getbitmap(0xFE00FF);
		var basetgood : Bitmap;
		var offsetx : Int = 0;
		var offsety : Int = 0;

		for (y in 0...myshape.length)
		{
			for (x in 0...myshape[0].length)
			{
				if (myshape[y][x]==2)
					{
						offsetx = -x;
						offsety = -y;
					}					
			}
		}
				
		basetgood = basetbad;

		for (y in 0...myshape.length)
		{
			for (x in 0...myshape[0].length)
			{
				if (myshape[y][x]!=0 &&
					Graph.inbounds(HUD.ctx+offsetx+x, HUD.cty+offsety+y, Game.cur.map) &&
					FortressManager.nearfort(HUD.ctx+offsetx+x, HUD.cty+offsety+y))
					basetgood = Images.getbitmap(0xFD00FF);				
			}
		}

		
		for (y in 0...myshape.length)
		{
			for (x in 0...myshape[0].length)
			{

			if (myshape[y][x]!=0)
				{				
				var newt : Bitmap = new Bitmap();

				var tx : Int = HUD.ctx+x+offsetx;
				var ty : Int = HUD.cty+y+offsety;
				
				if (FortressManager.test(tx,ty))
				{
					newt.bitmapData = basetgood.bitmapData.clone();
				}
				else
				{
					newt.bitmapData = basetbad.bitmapData.clone();
				}
				
				var np : Point = IsoMetrics.tiletopix_xy(tx,ty);
				
				newt.x = np.x;
				newt.y = np.y;
				
				this.addChild(newt);
				}
			}
		}	
		
		#end

	}
	
}
